
/**
 * 心灵氧吧模块入口
 * Created by alice on 2016/7/11.
 */
require.config({
    baseUrl:'../js',
    paths:{
        "getData":"Util/getData",
        "config":"common/config",
        "components":"common/components",
        "common":"common/common",
        "jquery":"lib/jquery.min",
        "wx":"lib/jweixin-1.0.0"
    },
    shim : {
        "components": { deps : ["jquery"] }
    },
    urlArgs: "v=" + (new Date()).getTime()//以禁用浏览器缓存,上线后注释掉
});
require(['jquery','getData','config','common','components','wx'],function($,getData,config,common,components,wx){

    var gm=common.gm;

    $(".listview_img_title_config li").ontouch(function(){
        var getvalue=$(this).attr("value");
        if(getvalue!=null && getvalue!="zycs")
            gm.pul.toUrl("http://weixin.hxqydyl.com/wxsupport/pages/zycs/zycsDirPage.html?appusr=1234123423");//专业测试
        else if(getvalue!="qwcs")
            gm.pul.toUrl("http://weixin.hxqydyl.com/wxsupport/pages/qwcs/qwcsDirPage.html?appusr=1234213424234");//趣味测试
        else
            console.log("gg");
    },true);

    gm.ad.get("doctorLunBoTuId",function(data){
        if(!data){
            $.alert("服务器出错!");
        }
        if(data.code == 200){
            var arr = [];

            //如果无数据，则出现默认数据
            if(data.value.length == 0){
                focusbind();
                return;
            }

            $(data.value).each(function(i,o){
                if(o.url){
                    o.url =  ' href="'+o["url"]+'"';
                }
                arr.push('<li><b'+o.url+'><img src="'+o["imageUrl"]+'"></b></li>');
            });

            $(".atlas").html(arr.join(""));

            $(".atlas b").ontouch(function(e){
                var href = $(this).attr("href");
                if(!href)return;
                gm.pul.openurl(href);
            },true);

            focusbind();
        }else{
            $.alert(data.message);
        }
    });

    function focusbind(){
        //对象&变量 缓存
        var $tab = $("#tabs"),
            $ul = $tab.find("ul"),
            $li = $ul.children(),
            $nav = $("<nav>"),
            li_len = $li.length,
            li_w = $li.eq(0).width();

        //容器 初始化
        $li.css({
                "float" : "left",
                "width" : li_w
            })
            .each(function(i){
                var str = i == 0 ? "<i class=\"cur\">" : "<i>";
                $nav.append(str);
            });
        $nav.appendTo($tab);
        $li.eq(0).clone().appendTo($ul);
        $li.eq(-1).clone().css("marginLeft",-li_w).prependTo($ul);
        $ul.css("float","left").width(li_w * $ul.children().length);

        //切换 方法
        var eventOff,startX,clientX,
            index = 0,
            eventMaxWidth = li_w,
            objStatus = {},
            eventTo,
            time=5000,
            tab = {
                //开始
                start : function (event){
                    //重置水平位置
                    clientX = 0;
                    //滑动激活
                    eventOff = true;
                    //获取鼠标||触点水平坐标
                    var touch = event.touches && event.touches[0] || event;
                    //记录初始坐标
                    startX = touch.clientX;
                    //记录切换前对象状态
                    objStatus.left = $ul.get(0).style.marginLeft;
                    //阻止对象默认行为
                    // event.preventDefault();
                    clearInterval(eventTo);
                },
                //移动
                move : function (event){
                    var touch = event.touches && event.touches[0] || event,
                        _x = touch.clientX - startX;
                    if(eventOff){
                        clientX = parseInt(_x/eventMaxWidth * 100);
                        $ul.get(0).style.marginLeft = (li_w * clientX / 100) - li_w * index + "px";
                        //console.log("滑动进度：" + clientX + "%");
                    }
                    event.preventDefault();
                },
                //结束
                end : function (event){
                    var _clientX = clientX > 0 ? clientX : -(clientX);
                    //滑动激活，且滑动百分比超过50
                    if(eventOff){
                        if(_clientX >= 30){
                            index = clientX > 0 ? index - 1 : index + 1;
                            tab.to();
                            console.log("切换完成");
                        }
                        else{
                            console.log("放弃切换");

                            $ul.animate({
                                "marginLeft" : objStatus.left
                            });
                        }

                        eventOff = false;
                    }
                    eventTo = setInterval(function(){
                        index++;
                        tab.to();
                    },time);
                    // event.preventDefault();
                },
                to:function(){
                    var moveCallback;
                    objStatus.left = -(li_w * index) + "px";
                    //复位
                    if(index >= li_len){
                        index = 0;
                        moveCallback = function(){
                            $ul.get(0).style.marginLeft = -(li_w * index) + "px";
                        };
                    }
                    else if(index < 0){
                        index = li_len - 1;
                        moveCallback = function(){
                            $ul.get(0).style.marginLeft = -(li_w * index) + "px";
                        };
                    }
                    $nav.children().eq(index).addClass("cur").siblings().removeClass("cur");
                    $ul.animate({
                        "marginLeft" : objStatus.left
                    },moveCallback);
                }
            };

        eventTo = setInterval(function(){
            index++;
            tab.to();
        },time);

        //触摸事件 注册
        $tab.get(0).addEventListener("touchstart",tab.start,false);
        $tab.get(0).addEventListener("touchmove",tab.move,false);
        $tab.get(0).addEventListener("touchend",tab.end,false);
    }


});

